#include "georowwidget.h"

GeoRowWidget::GeoRowWidget()
{
this->setAttribute(Qt::WA_DeleteOnClose);
setupUi(this);
readSettings();

current_record = QSqlRecord();
current_index = QModelIndex();
is_new = false;

dateEdit->setDate( QDate::currentDate() );

QObject::connect(commitButton,
                 SIGNAL(clicked()),
                 this,
                 SLOT(save_clicked()));
QObject::connect(cancelButton,
                 SIGNAL(clicked()),
                 this,
                 SLOT(close()));
}

GeoRowWidget::~GeoRowWidget()
{
}

void GeoRowWidget::set_record( QSqlRecord record,
                               QModelIndex index )
{
    if (!record.isNull("SPOT_NAME") )
    {
    QString s = record.value("SPOT_NAME").toString();
    geoNameCheckBox->setChecked(true);
    geoNameLineEdit->setText(s);
    }
    else
    {
    geoNameCheckBox->setChecked(false);
    geoNameLineEdit->clear();
    }

    if (!record.isNull("NOTE") )
    {
    QString s = record.value("NOTE").toString();
    geoNoteCheckBox->setChecked(true);
    geoNoteLineEdit->setText(s);
    }
    else
    {
    geoNoteCheckBox->setChecked(false);
    geoNoteLineEdit->clear();
    }

    if (!record.isNull("COORD") )
    {
    QString s = record.value("COORD").toString();
    geoCoordCheckBox->setChecked(true);
    geoCoordLineEdit->setText(s);
    }
    else
    {
    geoCoordCheckBox->setChecked(false);
    geoCoordLineEdit->clear();
    }

    if (!record.isNull("LATITUDE") )
    {
    latitudeCheckBox->setChecked(true);
    latitudeSpinBox->setValue( record.value("LATITUDE").toDouble() );
    }
    else
    {
    latitudeCheckBox->setChecked(false);
    }

    if (!record.isNull("LONGITUDE") )
    {
    longitudeCheckBox->setChecked(true);
    longitudeSpinBox->setValue( record.value("LONGITUDE").toDouble() );
    }
    else
    {
    longitudeCheckBox->setChecked(false);
    }

    if (!record.isNull("SPOT_DATE") )
    {
    QDate date = QDate::fromString( QString::number( record.value("SPOT_DATE").toInt() ),
                                    "yyyyMMdd" );
        if ( true == date.isValid() )
        {
        geoDateCheckBox->setChecked(true);
        dateEdit->setDate(date);
        }
    }
    else
    {
    geoDateCheckBox->setChecked(false);
    }

current_record = QSqlRecord(record);
current_index = QModelIndex(index);
}

QSqlRecord GeoRowWidget::read_record(QSqlRecord record)
{
    if ( geoNameCheckBox->isChecked() )
    {
    record.setValue( "SPOT_NAME", QVariant( geoNameLineEdit->text() ) );
    }
    else
    {
    record.setNull("SPOT_NAME");
    }

    if ( geoNoteCheckBox->isChecked() )
    {
    record.setValue("NOTE", QVariant( geoNoteLineEdit->text() ) );
    }
    else
    {
    record.setNull("NOTE");
    }

    if ( geoCoordCheckBox->isChecked() )
    {
    record.setValue("COORD", QVariant( geoCoordLineEdit->text() ) );
    }
    else
    {
    record.setNull("COORD");
    }

    if ( latitudeCheckBox->isChecked() )
    {
    double latitude = latitudeSpinBox->value();
    record.setValue("LATITUDE", QVariant(latitude) );
    }
    else
    {
    record.setNull("LATITUDE");
    }

    if ( longitudeCheckBox->isChecked() )
    {
    double longitude = longitudeSpinBox->value();
    record.setValue("LONGITUDE", QVariant(longitude) );
    }
    else
    {
    record.setNull("LONGITUDE");
    }

    if ( geoDateCheckBox->isChecked() )
    {
    QDate d = dateEdit->date();
    QString d_str = d.toString("yyyyMMdd");
    bool ok = false;
    int d_int = d_str.toInt( &ok, 10 );
        if ( true == ok )
        {
        record.setValue("SPOT_DATE", d_int );
        }
        else
        {
        record.setNull("SPOT_DATE");
        }
    }
    else
    {
    record.setNull("SPOT_DATE");
    }

return record;
}


void GeoRowWidget::new_record( qint64 coin_id, QString opened_album )
{

    if ( true == opened_album.isEmpty() )
    {
    return;
    }

QSqlRecord record = QSqlRecord();

qint64 spot_id = Q_INT64_C(-1);
    {
    QSqlDatabase db = QSqlDatabase::database("collection");
       if ( !db.isOpen() )
       {
       return;
       }
    QString s = QLatin1String("select gen_id(");
    s.append(opened_album);
    s.append(QLatin1String("_spots_gen,1) from rdb$database"));
    QSqlQuery query(s, db);
        while (query.next())
        {
        spot_id = query.value(0).toLongLong();
        }
    query.clear();
    }

    if (spot_id == -1)
    {
    return;
    }

is_new = true;

record.insert( 0, QSqlField("SPOT_ID", QVariant::LongLong) );
record.setValue("SPOT_ID", spot_id);

record.insert( 1, QSqlField("COIN_ID", QVariant::LongLong) );
record.setValue("COIN_ID", coin_id);

record.insert(  2, QSqlField("SPOT_NAME", QVariant::String) );
record.insert(  3, QSqlField("NOTE", QVariant::String) );
record.insert(  4, QSqlField("COORD", QVariant::String) );
record.insert(  5, QSqlField("LATITUDE", QVariant::Double) );
record.insert(  6, QSqlField("LONGITUDE", QVariant::Double) );
record.insert(  7, QSqlField("SPOT_DATE", QVariant::Int) );


current_record = QSqlRecord(record);
}

void GeoRowWidget::save_clicked()
{
QSqlRecord record = read_record(current_record);
emit record_to_commit( record,
                       current_index,
                       QLatin1String("SPOT_ID"),
                       is_new);
close();
}

void GeoRowWidget::writeSettings()
{
QSettings settings( QSettings::IniFormat,
                    QSettings::UserScope,
                    QApplication::organizationName(),
                    QApplication::applicationName() );
settings.setFallbacksEnabled(false);
settings.beginGroup( QLatin1String("GeoRowWidget") );
settings.setValue( QLatin1String("size"), QVariant( this->size() ) );
settings.setValue( QLatin1String("pos"), QVariant( this->pos() ) );
settings.endGroup();
}

void GeoRowWidget::readSettings()
{
QSettings settings( QSettings::IniFormat,
                    QSettings::UserScope,
                    QApplication::organizationName(),
                    QApplication::applicationName() );
settings.setFallbacksEnabled(false);                
settings.beginGroup( QLatin1String("GeoRowWidget") );
resize( settings.value( QLatin1String("size"), QSize( 400, 314 ) ).toSize() );
move( settings.value( QLatin1String("pos"), QPoint( 0, 0 ) ).toPoint() );
settings.endGroup();
}

void GeoRowWidget::closeEvent( QCloseEvent * e )
{
writeSettings();
e->accept();
}

